package com.tianqu.tms.core.webservice.weichat.api;

import com.aliyun.openservices.ons.api.Message;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.webservice.weichat.api.mq.StockMqConsumer;
import com.tianqu.tms.dto.MessageHistoryDto;
import com.tianqu.tms.entity.TmsMessageEntity;

import net.chedaiban.wechat.api.mq.protobuf.Rpc.RpcMessage;
import net.chedaiban.wechat.api.mq.protobuf.RpcServiceProto.CheckPhotographyInfo;
import net.chedaiban.wechat.api.mq.protobuf.RpcServiceProto.CollectCarInfo;
import net.chedaiban.wechat.api.mq.protobuf.RpcServiceProto.RegistrationCarInfo;
import net.chedaiban.wechat.api.mq.protobuf.RpcServiceProto.StockRegisterCarInfo;
import net.chedaiban.wechat.api.mq.protobuf.RpcServiceProto.TransferDataSyn000102;
import net.chedaiban.wechat.api.mq.protobuf.RpcServiceProto.TransferDataSyn000204;
import net.chedaiban.wechat.api.mq.protobuf.RpcServiceProto.TransferDataSyn000207;
import net.chedaiban.wechat.api.mq.protobuf.RpcServiceProto.TransferDataSyn000208;
import net.chedaiban.wechat.api.mq.protobuf.RpcServiceProto.TransferDataSyn000209;
import net.chedaiban.wechat.api.mq.protobuf.RpcServiceProto.TransferStockQXDataSyn000206;
import net.chedaiban.wechat.api.mq.protobuf.RpcServiceProto.TransferVinDataSyn000205;

public class StockMessageServer implements Listener{
	protected Logger LOGGER = Logger.getLogger(getClass());
	
	private static StockMqConsumer STOCK_CONSUMER = StockMqConsumer.getInstance();
	
	private static StockMessageServer INSTANCE;
	
	private StockMessageServer(){};
	
	public static StockMessageServer getInstance(){
		if(INSTANCE == null){
			INSTANCE = new StockMessageServer();
			STOCK_CONSUMER.addListener(INSTANCE);
		} 
		return INSTANCE;
	}
	
	public void start() throws Exception{
		STOCK_CONSUMER.start();
	}
	 
	@Override
	public void onMessage(Message mqMessage, RpcMessage rpcMessage) {
		TmsMessageEntity messageEntity = TmsMessageEntity.getInstance();
		TxController controller = TxController.getTxController();
		try {
			boolean r = process(rpcMessage);
			LOGGER.debug("[stock]recive message,class:"+rpcMessage.getPayloadClass());
			
			MessageHistoryDto dto = new MessageHistoryDto();
			controller.begin();
			//
			dto.setAction(TmsMessageEntity.ACTION_RECEIVE);
			dto.setMessageId(mqMessage.getMsgID());
			dto.setPayloadClass(rpcMessage.getPayloadClass());
			dto.setPayloadData(rpcMessage.getPayloadData().toByteArray());
			dto.setProtocol(TmsMessageEntity.PROTOCOL_PROTOBUF);
			if(r) dto.setResult(TmsMessageEntity.RESULT_OK);
			else dto.setResult(TmsMessageEntity.RESULT_NG);
			dto.setResultMessage("");
			dto.setType(TmsMessageEntity.TYPE_TMS);
			dto.setUpdateUserId(1);
			//
			messageEntity.insert(dto);
			controller.commit();
		} catch (Exception e) {
			LOGGER.error("MessageServer error", e);
			try {
				controller.rollback(); } catch (TransactionException e1) { LOGGER.error("failted to rollback db",e1);
			}
		}
		
	}

	private boolean process(RpcMessage msg){
		boolean r = true;
		try {
			if ("CollectCarInfo".equals(msg.getPayloadClass())) {
				CollectCarInfo collectCarInfo = CollectCarInfo.parseFrom(msg.getPayloadData().toByteArray());
				TmsConsumerService.getInstance().executeCollectCarInfo(collectCarInfo);
			} else if ("RegistrationCarInfo".equals(msg.getPayloadClass())) {
				RegistrationCarInfo registrationCarInfo = RegistrationCarInfo.parseFrom(msg.getPayloadData().toByteArray());
				TmsConsumerService.getInstance().executeRegistrationCarInfo(registrationCarInfo);
			} else if ("CheckPhotographyInfo".equals(msg.getPayloadClass())) {
				CheckPhotographyInfo checkPhotographyInfo = CheckPhotographyInfo.parseFrom(msg.getPayloadData().toByteArray());
				TmsConsumerService.getInstance().executeCheckPhotographyInfo(checkPhotographyInfo);
			} else if ("StockRegisterCarInfo".equals(msg.getPayloadClass())) {
				StockRegisterCarInfo stockRegisterCarInfo = StockRegisterCarInfo.parseFrom(msg.getPayloadData().toByteArray());
				TmsConsumerService.getInstance().executeStockRegisterCarInfo(stockRegisterCarInfo);
			}
			else if ("TransferDataSyn000204".equals(msg.getPayloadClass())) {
				TransferDataSyn000204 transferDataSyn000204 =TransferDataSyn000204.parseFrom(msg.getPayloadData().toByteArray());
				TmsConsumerService.getInstance().executeTransferDataSyn000204(transferDataSyn000204);
			}

			else if ("TransferVinDataSyn000205".equals(msg.getPayloadClass())) {
				TransferVinDataSyn000205 transferVinDataSyn000205 =TransferVinDataSyn000205.parseFrom(msg.getPayloadData().toByteArray());
				TmsConsumerService.getInstance().executeTransferVinDataSyn000205(transferVinDataSyn000205);
			}

			else if ("TransferStockQXDataSyn000206".equals(msg.getPayloadClass())) {
				TransferStockQXDataSyn000206 transferStockQXDataSyn000206 =TransferStockQXDataSyn000206.parseFrom(msg.getPayloadData().toByteArray());
				TmsConsumerService.getInstance().executeTransferStockQXDataSyn000206(transferStockQXDataSyn000206);
			}

			else if ("TransferDataSyn000207".equals(msg.getPayloadClass())) {
				TransferDataSyn000207 transferDataSyn000207 =TransferDataSyn000207.parseFrom(msg.getPayloadData().toByteArray());
				TmsConsumerService.getInstance().executeTransferDataSyn000207(transferDataSyn000207);
			}

			else if ("TransferDataSyn000208".equals(msg.getPayloadClass())) {
				TransferDataSyn000208 transferDataSyn000208 =TransferDataSyn000208.parseFrom(msg.getPayloadData().toByteArray());
				TmsConsumerService.getInstance().executeTransferDataSyn000208(transferDataSyn000208);
			}

			else if ("TransferDataSyn000209".equals(msg.getPayloadClass())) {
				TransferDataSyn000209 transferDataSyn000209 =TransferDataSyn000209.parseFrom(msg.getPayloadData().toByteArray());
				TmsConsumerService.getInstance().executeTransferDataSyn000209(transferDataSyn000209);
			}
			else if ("TransferDataSyn000102".equals(msg.getPayloadClass())) {
				TransferDataSyn000102 transferDataSyn000102 =TransferDataSyn000102.parseFrom(msg.getPayloadData().toByteArray());
				TmsConsumerService.getInstance().executeTransferDataSyn000102(transferDataSyn000102);
			}
		} catch (Exception e) {
			r = false;
			LOGGER.error("MessageServer error", e);
		}
		return r;
	}
	
	
	public static void main(String[] args) throws Exception {
		StockMessageServer.getInstance().start();
	}
}
